var totalPage = 0;
var totalCnt = 0;
var chargingStationData = [];
var equipmentData = [];
var detailTimeout, detailIndex;
var equipmentStatusCharts;
var countData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	createTableHead("realTimeStatusThead", equipmentRealTimeStatusTable, turnPageSearch);
	scrollTableSuspend("scrollBody", "realTimeStatusTable", "realTimeStatusTable1", "realTimeStatusThead", "scrollHeight");
	setBaseUrl();
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	equipmentStatusCharts();
	search();
	setTimeout(hideLoading,1500);
})
function getChargingStation(data) {
	chargingStationData = data;
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function equipmentStatusCharts(){
	equipmentStatusCharts = echarts.init(document.getElementById("stationEcharts"));
	equipmentStatusCharts.setOption(searchEcharts("vertical", "center", 50, 0, "统计信息", [{value: 0, name: "0", "itemStyle": echartStyle('#EAEAEA', false, false, '#EAEAEA')}]));
}
function searchRealTimeStatus(data){
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	equipmentData = data && data.equipmentStatusList || [];
	var length = equipmentData.length;
	for (var i = 0;i < length; i++){
		equipmentData[i].chargingStationName = '<a class="details" onclick="chargingStationName(' + i + ')">'+equipmentData[i].chargingStationName+'</a>'; 
	}
	createTableBody("realTimeStatusTbody", equipmentRealTimeStatusTable, equipmentData, true);
	if (data == "error" || data == 'timeout') {
		$("#ChargingStationID").empty();
		$("#ChargingStationID").append("<option value=''>请选择</option>");
		$("#communicationState").empty();
		$("#communicationState").append("<option value=''>请选择</option>");
	} else {
		if (chargingStationData && chargingStationData.length > 0) {
			if ($("#ChargingStationID option").size() == 1) {
				var length = chargingStationData.length;
				for (var i = 0; i < length; i++) {
					$("#ChargingStationID").append("<option value='" + chargingStationData[i].chargingStationID + "'>" + chargingStationData[i].chargingStationName + "</option>");
				}
			}
			if ($("#communicationState option").size() == 1) {
				$("#communicationState").append("<option value='1'>正常</option>");
				$("#communicationState").append("<option value='0'>断开</option>");
			}
		}
	}
	countData = [];
	countData.push({"value": (data.totalChargingCount || 0), 
		"name":'充电中:' + (data.totalChargingCount || 0), 
		"itemStyle": echartStyle('#5EE19F', true, true,'#5EE19F')
		});
	countData.push({"value": (data.totalChargedCount || 0), 
		"name":'充电结束:' + (data.totalChargedCount || 0), 
		"itemStyle": echartStyle('#58BCE0', true, true,'#58BCE0')
		});
	countData.push({"value": (data.totalIdleCount || 0), 
		"name":'空闲:' + (data.totalIdleCount || 0), 
		"itemStyle": echartStyle('#F1AC59', true, true,'#F1AC59')
		});
	countData.push({"value": (data.totalFaultCount || 0), 
		"name":'故障:' + (data.totalFaultCount || 0) ,
		"itemStyle": echartStyle('#FF6B6B', true, true,'#FF6B6B')
		});
	option.series[0].data = countData;
	option.legend.data = ['充电中:' + (data.totalChargingCount || 0), 
             			 '充电结束:' + (data.totalChargedCount || 0), 
            			 '空闲:' + (data.totalIdleCount || 0), 
            			 '故障:' + (data.totalFaultCount || 0)];
	equipmentStatusCharts.setOption(option, true);
	$("#svg").html('');
	$("#monitorings").html('');
	$("#noMonitoring").html('');
	$("#svg").html(
		 '<svg style="position:absolute;width:110px;">' +
            '<circle cx="55" cy="70" r="55" style="fill:#5EE19F;" stroke="white"></circle>' +
            '<text x="'+ (50-(data.totalOnlineCount && data.totalOnlineCount.toString().length || 1)*5) +'" y="80" font-size="30" style="fill:white">' + (data.totalOnlineCount || 0) + '</text>' +
         '</svg>' +
         '<svg style="width:80px;margin-left:90px;">' +
             '<circle cx="40" cy="70" r="40" style="fill:#58BCE0;" stroke="white"></circle>' +
             '<text x="'+ (40-(data.totalOfflineCount && data.totalOfflineCount.toString().length || 1)*6) +'" y="80" font-size="24" style="fill:white">' + (data.totalOfflineCount || 0) + '</text>' +
     	'</svg>'
			);
	$("#monitorings").html(data.totalOnlineCount || 0);
	$("#noMonitoring").html(data.totalOfflineCount || 0);
	setTimeout(search, 2000);
}
function chargingStationName(num){
	window.parent.detalisDrop('chargingStation/chargingStationDetail', num, $("#currentPage").val(), "drownDown1", "" ,"chargingStationPage" ,"chargingStation");
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(equipmentData[num].chargingStationID);
}
function changeSelect() {
	showLoading();
	setTimeout(hideLoading, 2500);
}
function search(){
	$ajax('realTimeEquipment', 'equipmentStatusForm', searchRealTimeStatus, '', searchRealTimeStatus);
}
function turnPageSearch(){
	showLoading();
	setTimeout(hideLoading, 3000);
}
function details(index){
	window.parent.detalisSkip('chargingEquipment/equipmentRealTimeDetail',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("equipmentPage");
	$("#chargingStationID", parent.document).val(equipmentData[index].chargingStationID);
	$("#detailID", parent.document).val(equipmentData[index].equipmentNo);
}
